'use strict';

const express = require('express');
// const cheerio = require('cheerio'); // 解析HTML页面所需信息
const router = express.Router();
const https = require('https'); // 网络请求信息
// const { SuccessFun } = require('@/utils/utils');

/**
 * 获取指定网站表头信息
 * url 网站地址
 */
router.get('/website', (req, res) => {
  const url = req.query.url;
  if (!url) res.status(400).send({ message: '缺少url参数' });

  const opt = {
    hostname: url,
    port: 443,
    path: '/',
    method: 'GET',
  };

  const http = https.request(opt, (response) => {
    console.log('response', response);

    response.on('data', (data) => {
      console.log(data);
    });
  });

  http.on('error', (error) => {
    console.error('error', error);
  });

  http.end();

  // axios
  //   .get(siteUrl)
  //   .then((response) => {
  //     const $ = cheerio.load(response.data);
  //     const title = $('title').text();
  //     const favicon = $('link[rel="shortcut icon"]').attr('href');
  //     const canonical = $('link[rel="canonical"]').attr('href');
  //     const keywords = $('meta[name="keywords"]').attr('content');
  //     const description = $('meta[name="description"]').attr('content');
  //     const urlObj = new URL(favicon, siteUrl);
  //     const faviconUrl = urlObj.href;

  //     const result = SuccessFun(req, {
  //       title: !title ? '' : title,
  //       favicon: !favicon ? '' : faviconUrl,
  //       keywords: !keywords ? '' : keywords,
  //       description: !description ? '' : description,
  //       canonical: !canonical ? '' : canonical,
  //     });

  //     res.send(result);
  //   })
  //   .catch((error) => {
  //     res.status(500).send(error.message);
  //   });

  return;
});

// baseUrl
// originalUrl
// pathname
// params
// query

module.exports = router;
